﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class ZHtest1
    {
        public static void TriangularInsert(Dictionary<string, Object> data)    //正三边形刀片，适用于T形刀片
        {
            DxfDocument dxf = new DxfDocument();




            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer center = new Layer("center") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线

            double tic = Convert.ToDouble(data["ic"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            if (scale == 0 && tic < 9)
            {
                scale = 3.0;
            }
            else if (scale == 0 && tic < 20)
            {
                scale = 2.5;
            }
            else if (scale == 0)
            {
                scale = 2.0;
            }


            double pi = Math.PI;
            double DIM = Convert.ToDouble(data["dim"]);
            double ic = Convert.ToDouble(data["ic"]) * scale;
            double s = Convert.ToDouble(data["s"]) * scale;
            double d1 = Convert.ToDouble(data["d1"]) * scale;
            double re = Convert.ToDouble(data["re"]) * scale;
            double epsr = Convert.ToDouble(data["epsr"]) * pi / 180;
            double an = Convert.ToDouble(data["an"]) * pi / 180;
            // int ifs = (int)Convert.ToDecimal(data["ifs"]);
            int cb = (int)Convert.ToDecimal(data["cb"]);
            string path = Convert.ToString(data["path"]);
            double le = 2 * ic * Math.Cos(pi / 6);
            double ictc = Convert.ToDouble(data["ictc"]);
            double stc = Convert.ToDouble(data["stc"]);
            //double d1tc = Convert.ToDouble(data["d1tc"]);
            double dxc = 0.8;

            Vector2 p1 = new Vector2(0, 0);
            Vector2 p0 = fun.Polar(p1, pi / 6, ic);
            Vector2 p2 = fun.Polar(p1, 0, le);
            Vector2 p3 = fun.Polar(p1, pi / 3, le);
            Vector2 p1p = fun.Polar(p1, pi / 6, 2 * re);
            Vector2 p2p = fun.Polar(p2, 5 * pi / 6, 2 * re);
            Vector2 p3p = fun.Polar(p3, -0.5 * pi, 2 * re);
            Vector2 p4 = fun.Polar(p1p, 5 * pi / 6, re);
            Vector2 p5 = fun.Polar(p1p, -0.5 * pi, re);
            Vector2 p6 = fun.Polar(p2p, -0.5 * pi, re);
            Vector2 p7 = fun.Polar(p2p, pi / 6, re);
            Vector2 p8 = fun.Polar(p3p, pi / 6, re);
            Vector2 p9 = fun.Polar(p3p, 5 * pi / 6, re);

            Vector2 p20 = fun.Polar(p0, 0, 2 * ic);
            Vector2 p21 = new Vector2(p20.X, p3p.Y + re);

            Vector2 p22 = fun.Polar(p21, pi + an, s / Math.Cos(an));
            Vector2 p24 = new Vector2(p20.X, p1.Y);
            Vector2 p23 = fun.Polar(p24, pi - an, s / Math.Cos(an));

            Vector2 v1 = fun.Polar(p0, -150 * pi / 180, ic / 2 / Math.Cos(60 * pi / 180) * dxc);
            Vector2 v2 = fun.Polar(p0, -30 * pi / 180, ic / 2 / Math.Cos(60 * pi / 180) * dxc);
            Vector2 v3 = fun.Polar(p0, 90 * pi / 180, ic / 2 / Math.Cos(60 * pi / 180) * dxc);





            if (cb == 1 || cb == 2)
            {


                Line line15 = new Line(v1, v2);
                line15.Lineweight = Lineweight.W25;
                line15.Layer = additionalline;
                dxf.AddEntity(line15);
                Line line16 = new Line(v2, v3);
                line16.Lineweight = Lineweight.W25;
                line16.Layer = additionalline;
                dxf.AddEntity(line16);
                Line line17 = new Line(v1, v3);
                line17.Lineweight = Lineweight.W25;
                line17.Layer = additionalline;
                dxf.AddEntity(line17);

            }



            //inserthole(dxf, p0, v2, v3, p20, p21, scale, data);

            Arc arc3 = fun.DrawArc(p8, p3p, p9);

            LwPolyline poly11 = new LwPolyline();
            poly11.Vertexes.Add(new LwPolylineVertex(p4));
            poly11.Vertexes[0].Bulge = Math.Tan((pi - epsr) / 4);
            poly11.Vertexes.Add(new LwPolylineVertex(p5));
            poly11.Vertexes.Add(new LwPolylineVertex(p6));
            poly11.Vertexes[2].Bulge = Math.Tan((pi - epsr) / 4);
            poly11.Vertexes.Add(new LwPolylineVertex(p7));
            poly11.Vertexes.Add(new LwPolylineVertex(p8));
            poly11.Vertexes[4].Bulge = Math.Tan((pi - epsr) / 4);
            poly11.Vertexes.Add(new LwPolylineVertex(p9));
            poly11.IsClosed = true;
            fun.PolyFill(dxf, poly11, 0.5, contour);

            Line line12 = new Line(p1, p2);
            line12.Lineweight = Lineweight.W25;
            line12.Layer = additionalline;
            dxf.AddEntity(line12);
            Line line13 = new Line(p2, p3);
            line13.Lineweight = Lineweight.W25;
            line13.Layer = additionalline;
            dxf.AddEntity(line13);
            Line line14 = new Line(p1, p3);
            line14.Lineweight = Lineweight.W25;
            line14.Layer = additionalline;
            dxf.AddEntity(line14);



            Line cenline1 = new Line(fun.Polar(p0, pi, ic / 2 * 1.5), fun.Polar(p0, 0, ic / 2 * 1.5));
            Line cenline2 = new Line(fun.Polar(p0, 0.5 * pi, ic * 1.1), fun.Polar(p0, -0.5 * pi, ic / 2 * 1.5));
            Line cenline3 = new Line(fun.Polar(p20, 0, 3), fun.Polar(p20, pi, s + 3));
            Circle dotcircle1 = new Circle(p0, ic / 2);
            cenline1.Layer = center;
            cenline2.Layer = center;
            cenline3.Layer = center;
            dotcircle1.Layer = refin;
            cenline1.Linetype = Linetype.Center;
            cenline2.Linetype = Linetype.Center;
            cenline3.Linetype = Linetype.Center;
            dotcircle1.Linetype = Linetype.DashDot;
            dxf.AddEntity(cenline1);
            dxf.AddEntity(cenline2);
            dxf.AddEntity(cenline3);
            dxf.AddEntity(dotcircle1);

            fun.jz(dxf, fun.Polar(fun.Polar(p0, 0, ic / 2), -90 * pi / 180, 25), 0, 3, "A");

            DimensionStyle dimensionStyle = new DimensionStyle("sd");
            dimensionStyle.ArrowSize = 1.6;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            dimensionStyle.TextHeight = 1.2;
            double offset = 10;
            //标注
            if (DIM == 0)
            {
                LinearDimension dim1 = new LinearDimension(new Line(fun.Polar(p0, pi, ic / 2), fun.Polar(p0, 0, ic / 2)), offset * 2.5, 180, dimensionStyle);
                dim1.UserText = "IC";
                dxf.AddEntity(dim1);

                LinearDimension dim2 = new LinearDimension(new Line(p23, p24), offset, 180, dimensionStyle);
                dim2.UserText = "S";
                dxf.AddEntity(dim2);

                if (re / scale != 0.001)
                {
                    RadialDimension dimrad1 = new RadialDimension(arc3, 10, offset, dimensionStyle);//DimensionStyle.Default);
                    dimrad1.UserText = "RE";
                    dxf.AddEntity(dimrad1);
                }

                LinearDimension dim5 = new LinearDimension(new Line(p1, p2), offset * 2.25, 180, dimensionStyle);
                dim5.UserText = "LE";
                dxf.AddEntity(dim5);

                if (an != 0)
                {/*
                    Angular2LineDimension dimang2 = new Angular2LineDimension(new Line(p23, fun.Polar(p23, pi - an, 1)), new Line(p24, fun.Polar(p24, pi, 1)), s + offset, dimensionStyle);
                    dimang2.UserText = "AN=" + an * 180 / pi + "°";
                    dxf.AddEntity(dimang2);*/
                    fun.jdbz2(dxf, new Line(p23, fun.Polar(p23, pi - an, 1)), new Line(p24, fun.Polar(p24, pi, 1)), s + offset, 1.2, 1.6, 0.5, "AN=" + an * 180 / pi + "°", "", "");

                }
            }
            else
            {
                if (ictc == 0)
                {
                    LinearDimension dim1 = new LinearDimension(new Line(fun.Polar(p0, pi, ic / 2), fun.Polar(p0, 0, ic / 2)), offset * 2.5, 180, dimensionStyle);
                    dim1.UserText = String.Format("IC={0}", ic / scale);
                    dxf.AddEntity(dim1);
                }
                if (ictc != 0)
                {
                    LinearDimension dim1 = new LinearDimension(new Line(fun.Polar(p0, pi, ic / 2), fun.Polar(p0, 0, ic / 2)), offset * 2.5, 180, dimensionStyle);
                    dim1.UserText = String.Format("IC={0}±{1}", ic / scale, ictc);
                    dxf.AddEntity(dim1);
                }
                if (stc == 0)
                {
                    LinearDimension dim2 = new LinearDimension(new Line(p23, p24), offset, 180, dimensionStyle);
                    dim2.UserText = String.Format("S={0}", s / scale);
                    dxf.AddEntity(dim2);
                }
                if (stc != 0)
                {
                    LinearDimension dim2 = new LinearDimension(new Line(p23, p24), offset, 180, dimensionStyle);
                    dim2.UserText = String.Format("S={0}±{1}", s / scale, stc);
                    dxf.AddEntity(dim2);
                }


                if (re / scale != 0.001)
                {
                    RadialDimension dimrad1 = new RadialDimension(arc3, 10, offset, dimensionStyle);//DimensionStyle.Default);
                    dimrad1.UserText = String.Format("RE={0}", re / scale);
                    dxf.AddEntity(dimrad1);
                }
                LinearDimension dim5 = new LinearDimension(new Line(p1, p2), offset * 2.25, 180, dimensionStyle);
                dim5.UserText = String.Format("LE={0}", Math.Round(le / scale, 2));
                dxf.AddEntity(dim5);

                if (an != 0)
                {
                    /*Angular2LineDimension dimang2 = new Angular2LineDimension(new Line(p23, fun.Polar(p23, pi - an, 1)), new Line(p24, fun.Polar(p24, pi, 1)), s + offset, dimensionStyle);
                    dimang2.UserText = "AN=" + an * 180 / pi + "°";
                    dxf.AddEntity(dimang2);*/
                    fun.jdbz2(dxf, new Line(p23, fun.Polar(p23, pi - an, 1)), new Line(p24, fun.Polar(p24, pi, 1)), s + offset, 1.2, 1.6, 0.5, "AN=" + an * 180 / pi + "°", "", "");


                }
            }

            double isframe = Convert.ToDouble(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            if (isframe == 1)
            {
                //Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 90, p0.Y + 120), data);
                //fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");

                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}